package com.larry.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.io.IOException;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 10:17 2021/12/9
 * @Description TODO
 **/
public class Hbase_DDL {

    private static Connection connection;

    static {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","hadoop102,hadoop103,hadoop104");

        try {
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Admin admin;

    /**
     * 创建namespace
     * @param namespace
     */
    public static void createNameSpace(String namespace) throws IOException {
        //获取admin对象
        Admin admin = connection.getAdmin();

        NamespaceDescriptor descriptor = NamespaceDescriptor.create(namespace).build();

        admin.createNamespace(descriptor);

        admin.close();
    }

    /**
     * 创建table
     * @param nameSpaceName
     * @param tableName
     * @param cfs
     */
    public static void createTable(String nameSpaceName,String tableName,String ... cfs) throws IOException {
        try {
            admin = connection.getAdmin();

            TableName name = TableName.valueOf(nameSpaceName,tableName);
            TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(name);

            for (String cf : cfs) {
                ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
                ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();
                tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
            }

            TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
            admin.createTable(tableDescriptor);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            admin.close();
        }

    }

    /**
     * 判断表是否存在
     * @param nameSpaceName
     * @param tableName
     */
    public static void existTable(String nameSpaceName,String tableName) throws IOException {
        admin = connection.getAdmin();
        TableName name = TableName.valueOf(nameSpaceName, tableName);
        boolean b = admin.tableExists(name);
        System.out.println(b);

        admin.close();
    }

    /**
     * 删除表
     * @param nameSpaceName
     * @param tableName
     */
    public static void dropTable(String nameSpaceName,String tableName) throws IOException {
        admin = connection.getAdmin();

        TableName name = TableName.valueOf(nameSpaceName, tableName);
        admin.disableTable(name);
        admin.deleteTable(name);

        admin.close();
    }
}
